package com.zhou.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.framework.util.BlackListUtil;
import com.zhou.util.NumberUtil;
import com.zhou.util.StringTool;
import com.zhou.org.dao.BlackList;
import com.zhou.org.mapper.BlackListMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.org.service.BlackListService;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lang.zhou
 * @since 2022-05-06
 */
@Service
public class BlackListServiceImpl extends ServiceImpl<BlackListMapper, BlackList> implements BlackListService {

    @Override
    public BlackList getByIp(String ip) {
        LambdaQueryWrapper<BlackList> wrap = new LambdaQueryWrapper<>();
        wrap.eq(BlackList::getHost,ip);
        return baseMapper.selectOne(wrap);
    }

    @Override
    public List<BlackList> list(Map<String, Object> map) {
        String ip = MapUtils.getString(map,"ip","");
        LambdaQueryWrapper<BlackList> wrap = new LambdaQueryWrapper<>();
        if(StringTool.isNotBlank(ip)){
            wrap.eq(BlackList::getHost,ip);
        }
        wrap.orderByDesc(BlackList::getCreateDt);
        return list(wrap);
    }

    @Override
    public void saveBlackItem(BlackList i) {
        if(!NumberUtil.upper0(i.getId())){
            if(i.getCreateDt() == null){
                i.setCreateDt(new Date());
            }
            save(i);
        }else{
            updateById(i);
        }
        BlackListUtil.putBlackItem(i);
    }
}
